<?php
class Newsletter extends Admin_Controller
{
	function __construct()
	{
		parent::__construct();
		remove_ssl();
		$this->auth->check_access('Admin', true);
		$this->load->model('newsletter_model');
		$this->load->model('Settings_model');
	}
		
	public function index()
	{
		$this->load->helper('form');

		$this->load->library('form_validation');
		
		$data	= self::listEmails();
		$data['page_title']		= lang('common_newsletter');
		
		$settings	= $this->Settings_model->get_settings('newsletter');
		if(!empty($settings)){
			$data['subjet']=$settings['subjet'];		
			$data['description']=$settings['description'];
		}else{
			$data=array(
					'subjet'=>'',
					'description'=>''
				);
		}
		$this->form_validation->set_rules('subjet', 'subjet', 'required');
		$this->form_validation->set_rules('description', 'description', 'required');
		
		if ($this->form_validation->run() == FALSE)
		{
			
		}
		else
		{
			if($this->input->post('save')){
				self::save();
				$this->load->view($this->config->item('admin_folder').'/newsletter', $data);
				return true;
			}
			if($this->input->post('send')){
				self::send();
				return true;
			}
			if($this->input->post('reset')){
				self::reset();
				return true;
			}
		}
	
		$this->load->view($this->config->item('admin_folder').'/newsletter', $data);
	}
	public function save(){
		$config['description']=$this->input->post('description');
		$config['subjet']=$this->input->post('subjet');
		$this->Settings_model->save_settings('newsletter', $config);
	}
	public function send(){

		$emails=explode(';', $this->input->post('emails'));
		$settings	= $this->Settings_model->get_settings('newsletter');
		if(!empty($settings)){
			$data['subjet']=$settings['subjet'];
			$data['description']=$settings['description'];
		
			$this->load->library('email');
			$this->email->from('contact@asieco.com', 'Asieco');		
			$this->email->subject($data['subjet']);
			$this->email->message($data['description']);
			
			if(!empty($emails)){
				foreach ($emails as $email){
					$this->newsletter_model->check_send($email);
					echo "<div class='alert alert-success'>$email a été envoié</div>";
					$this->email->to($email);
					$this->email->send();
				}
			}
		}else echo "<div class='alert alert-error'>Vous n'avez pas sauvegardé votre newsletter</div>";
	}
	public function listEmails( $page=0,$rows=150){
		
		$this->load->library('pagination');
		
		$data['total']	= $this->newsletter_model->get_newsletter_count();
		$config['base_url']			= "javascript:page('";//site_url($this->config->item('admin_folder').'/newsletter');
		$config['total_rows']		= $data['total'];
		$config['per_page']			= $rows;
		$config['uri_segment']		= 7;
		$config['first_link']		= 'First';
		$config['first_tag_open']	= '<li>';
		$config['first_tag_close']	= '</li>';
		$config['last_link']		= 'Last';
		$config['last_tag_open']	= '<li>';
		$config['last_tag_close']	= '</li>';
		
		$config['full_tag_open']	= '<div class="pagination"><ul>';
		$config['full_tag_close']	= '</ul></div>';
		$config['cur_tag_open']		= '<li class="active"><a href="#">';
		$config['cur_tag_close']	= '</a></li>';
		
		$config['num_tag_open']		= '<li>';
		$config['num_tag_close']	= '</li>';
		
		$config['prev_link']		= '&laquo;';
		$config['prev_tag_open']	= '<li>';
		$config['prev_tag_close']	= '</li>';
		
		$config['next_link']		= '&raquo;';
		$config['next_tag_open']	= '<li>';
		$config['next_tag_close']	= '</li>';

		$config['get_sort']      =  "')"; // default FALSE
		
		$this->pagination->initialize($config);
		
		$data['emails']		= $this->newsletter_model->get_newsletters($rows, $page);
		
		return $data;
	}
	public function pageEmails($page){
		$data=self::listEmails($page);
		if(!empty($data['emails'])){
			$this->pagination->cur_page=$page;
			echo '<div class="row">'.$this->pagination->create_links().'</div>';
			 foreach ($data['emails'] as $email):
				echo '<div class="input-group">
		            <span class="input-group-addon">
		              <input type="checkbox" checked value="'.$email->email.'">
		            </span>'.$email->email.'
		          </div>';
			endforeach;
			echo '<div class="row">'.$this->pagination->create_links().'</div>';
		}
	}

	public function allEmails(){
		$data=self::listEmails('');
		if(!empty($data['emails'])){
			foreach ($data['emails'] as $email):
				echo $email->email.';';
			endforeach;
		}
	}
	public function AddEmail(){

		if($this->input->post('addEmail')){
			$emails=explode(';',$this->input->post('emailsliste'));
			$emails=array_unique($emails);
		
		
			foreach ($emails as $email){
				if(trim($email)) $this->newsletter_model->insertEmail(trim($email));
			}
		}
		redirect('admin/newsletter');
	}

	public function reset(){
	
		$this->newsletter_model->reset();

		redirect('admin/newsletter');
	}
}